﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Reflection;
using PolePosition.v3.Desktop.Utility;

namespace PolePosition.v3.Desktop
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            bool _firstInstance = false;
            using (System.Threading.Mutex AppMutex = new System.Threading.Mutex(false, "Pole Position Desktop", out _firstInstance))
            {
                if (_firstInstance == true)
                {

                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);

                    SetupProxy();
                    VersionNumber = ProjectCommon2.Helpers.ConfigHelper.GetAppSetting("VersionNumber");

                    Application.Run(new FormMain());
                }
            } // Mutex will be disposed of here
        }

        private static void SetupProxy()
        {
            string _Environment;
            _Environment = ProjectCommon2.Helpers.ConfigHelper.GetAppSetting("Environment");

            string _key;
            _key = _Environment + "_BaseUrl";

            string _baseServiceURL = ProjectCommon2.Helpers.ConfigHelper.GetAppSetting(_key);
            PolePosition.v3.ServiceProxy.Setup.DefaultBinding = PolePosition.v3.ServiceProxy.Setup.BindingTypes.HTTP;
            PolePosition.v3.ServiceProxy.Setup.BaseUrl = _baseServiceURL;
        }

        internal static string VersionNumber { get; set; }

        internal static string BuildNumber
        {
            get
            {
                Version _Version = Assembly.GetExecutingAssembly().GetName().Version;
                return string.Format("{0}.{1}.{2}.{3}", _Version.Major, _Version.Minor, _Version.Revision, _Version.Build);
            }
        }

        internal static EntityLibrary.LookupTables LookupTableData { get; set; }

        internal static EntityLibrary.LoginEntity LoginResult { get; set; }

        /// <summary>
        /// Store information about the selections the users have made to interact with Map, Data, Reports, etc.
        /// This information is stored in user settings so that it will persist from one use to the next. If a 
        /// new session then these are the defaults. There may be a case that certian elements are not stored from
        /// session to session, in which case we don't store in user settings and simply uses the properties below
        /// to store it during the session
        /// </summary>
        internal static class UserSelections
        {
            public static EntityLibrary.ProjectEntity VendorSelectedProject 
            {
                get
                {
                    return Properties.Settings.Default.VendorSelectedProject;
                }
                set
                { 
                    Properties.Settings.Default.VendorSelectedProject = value;
                    Properties.Settings.Default.Save();
                }
            }

            /// <summary>
            /// This is used to identify the current utility context
            /// </summary>
            public static int UtilityId { get; set; }

            //todo: create a class to store the relevant data in
            //public static object VendorMapCriteria { get; set; }

            //todo: create a class to store the relevant data in
            //public static object VendorDataCriteria { get; set; }

            //public static string VendorLastReport { get; set; }

            private static Selectors.MapCriteria m_UtilityMapCriteria;
            /// <summary>
            /// Store the utility user selections for map selector
            /// </summary>
            public static Selectors.MapCriteria UtilityMapCriteria
            {
                get 
                {
                    if (m_UtilityMapCriteria == null)
                        m_UtilityMapCriteria = new Selectors.MapCriteria();

                    return m_UtilityMapCriteria; 
                } 
            }

            //todo: create a class to store the relevant data in
            //public static object UtilityDataCriteria { get; set; }

            //todo: create a class to store the relevant data in
            //public static object UtilityWorkQueueCriteria { get; set; }

            //public static string UtilityLastReport { get; set; }

            public static bool VendorSetupComplete { get; set; }

            public static string VendorSDPath
            {
                get
                {
                    return Properties.Settings.Default.VendorSDPath;
                }
                set
                {
                    Properties.Settings.Default.VendorSDPath = value;
                    Properties.Settings.Default.Save();
                }
            }
        }

        public static FormMain FormMain
        {
            get;
            set;
        }

        #region Settings

        public static string GetSettingValue(int settingId)
        {
            if (LookupTableData == null)
                return null;

            EntityLibrary.SettingEntity _Setting;
            _Setting = new EntityLibrary.SettingEntity() { SettingId = settingId };

            ProjectCommon2.Generics.EntityCollection<EntityLibrary.SettingEntity> _Settings;

            _Settings = LookupTableData.Settings.Filter(_Setting, ProjectCommon2.Enumerations.FilterOperator.None);

            if (_Settings == null || _Settings.Count() == 0)
                return null;
            else
                return _Settings[0].SettingValue;
        }

        public static string GetSettingValue(string settingKey)
        {
            if (LookupTableData == null)
                return null;

            EntityLibrary.SettingEntity _Setting;
            _Setting = new EntityLibrary.SettingEntity() { SettingKey = settingKey };

            ProjectCommon2.Generics.EntityCollection<EntityLibrary.SettingEntity> _Settings;

            _Settings = LookupTableData.Settings.Filter(_Setting, ProjectCommon2.Enumerations.FilterOperator.None);

            if (_Settings == null || _Settings.Count() == 0)
                return null;
            else
                return _Settings[0].SettingValue;
        }

        #endregion
    }
}
